import {
  otherRouter,
  appRouter
} from '@/router/router'
import * as types from '../types'
import axios from 'axios'

const state = {
  menuList: [],
  routers: [
    otherRouter,
    ...appRouter
  ],
  grades: [],
  subjects: [],
  editions: [],
  loaded: {
    grades: false,
    subjects: false,
    editions: false
  },
  uploadUrl: '',
  account: {
    role: 0,
    name: 'loading...',
    isAdmin: false,
    logged: false
  },
  cachePage: [],
  currentTitle: '',
  pageOpenedList: [{
    title: '首页',
    path: '/admin/index',
    name: 'dashboard'
  }],
  currentPath: [{
    title: '首页',
    path: '/admin/index',
    name: 'dashboard'
  }],
  tagsList: [...appRouter],
  dontCache: []
}

const mutations = {
  setCurrentPath(state, pathArr) {
    state.currentPath = pathArr
  },
  setCurrentPageName(state, name) {
    state.currentPageName = name
  },
  updateMenulist(state) {
    state.menuList = appRouter
  },
  [types.SETTINGS_LOADED](state, data) {
    state[data.key] = data.value
  },
  [types.SETTINGS_LOADING](state, key) {
    state.loaded[key] = true
  },
  [types.LOGGED](state, data) {
    state.grades = data.config.grades
    state.subjects = data.config.subjects
    state.editions = data.config.editions
    state.uploadUrl = data.config.uploadUrl

    state.loaded['grades'] = true
    state.loaded['subjects'] = true
    state.loaded['editions'] = true

    state.account.logged = true
    state.account.role = data.role
    state.account.name = data.name
    state.account.isAdmin = data.isAdmin
  },
  init(state) {
    state.account.logged = false
  }
}

const loadSettingItem = async (commit, key) => {
  if (state.loaded[key]) return
  commit(types.SETTINGS_LOADING, key)
  let actionName = key
  if (key === 'editions') actionName = 'textBookEditions'
  let response = await axios.get('/api/settings/' + actionName)
  commit(types.SETTINGS_LOADED, {
    key: key,
    value: response.data
  })
}

const actions = {
  async getLoggedInfo({
    state,
    commit
  }) {
    if (state.account.logged && localStorage['ticket']) return
    let response = await axios.get('/vts/account/logged')
    let data = response.data
    if (data.success) {
      commit(types.LOGGED, data.data)
    }
  },
  async loadGrades({
    commit,
    state
  }) {
    await loadSettingItem(commit, 'grades')
  },
  async loadEditions({
    commit,
    state
  }) {
    await loadSettingItem(commit, 'editions')
  },
  async loadSubjects({
    commit,
    state
  }) {
    await loadSettingItem(commit, 'subjects')
  },
  async addEvent({
    commit
  }, data) {
    const response = await axios.post('/vts/event/add', data.model)
    if (response.status === 200 && response.data.success !== false) data.callback(response.data)
  },
  async recommendComment({
    commit
  }, data) {
    await actions.addEvent({
      commit
    }, {
      model: {
        id: data.id,
        type: 'RecommendComment'
      },
      callback: data.callback
    })
  },
  async getCurrentRanking({
    commit
  }, data) {
    const response = await axios.get('/vts/rank/current')
    if (response.status === 200) data.callback(response.data)
  }
}

const getters = {
  grades: state => state.grades,
  subjects: state => state.subjects,
  editions: state => state.editions,
  uploadUrl: state => state.uploadUrl,
  account: state => state.account
}

export default {
  state,
  mutations,
  getters,
  actions
}
